package jp.tkym.labs.m3d;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jp.tkym.labs.geom.CoordinateModel;

class ModelDrawer{
	private AbstractModelingPoint3D rootPoint;
	private final Translator3D translator;
	private final List<Draw> drawingList = new ArrayList<Draw>();
	private ModelConverter modelConverter;
	
	ModelDrawer(ModelingPoint3D viewPoint, Translator3D translator){
		// search root point 
		this.rootPoint = viewPoint;
		while(rootPoint.parent != null)
			rootPoint = rootPoint.parent;
		this.translator = translator;
	}
	
	public List<Draw> draw(ModelConverter converter){
		this.modelConverter = converter;
		drawingList.clear();
		Map<String, ModelingPoint3D> map = this.rootPoint.pointMap;
		for(ModelingPoint3D child : map.values()) drawAll(child);
		return drawingList;
	}
	
	private void drawAll(ModelingPoint3D point){
		for(ModelingPoint3D child : point.pointMap.values())
			drawAll(child);
		for(Model3D model : point.objectList) 
			drawingList.add(
					drawItem(point.coordinateModel, model));
	}
	
	private Draw drawItem(CoordinateModel co, Model3D model){
		translator.local(co);
		if(this.modelConverter instanceof AbstractModelConverter)
			((AbstractModelConverter)this.modelConverter).setTranslator(translator);
		return this.modelConverter.convert(model);
	}
}